// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import org.joml.Vector3f;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.PerformanceSettings;
import zombie.core.math.PZMath;
import zombie.input.GameKeyboard;
import zombie.input.JoypadManager;
import zombie.input.Mouse;
import zombie.iso.sprite.IsoSprite;
import zombie.network.GameServer;
import zombie.ui.UIManager;
import zombie.vehicles.BaseVehicle;

public final class PlayerCamera {
    public final int playerIndex;
    public float OffX;
    public float OffY;
    public float TOffX;
    public float TOffY;
    public float lastOffX;
    public float lastOffY;
    public float RightClickTargetX;
    public float RightClickTargetY;
    public float RightClickX;
    public float RightClickY;
    private float RightClickX_f;
    private float RightClickY_f;
    public float DeferedX;
    public float DeferedY;
    public float zoom;
    public int OffscreenWidth;
    public int OffscreenHeight;
    private static final Vector2 offVec = new Vector2();
    private static float PAN_SPEED = 1.0F;
    private long panTime = -1L;
    private final Vector3f m_lastVehicleForwardDirection = new Vector3f();

    public PlayerCamera(int _playerIndex) {
        this.playerIndex = _playerIndex;
    }

    public void center() {
        float float0 = this.OffX;
        float float1 = this.OffY;
        if (IsoCamera.CamCharacter != null) {
            IsoGameCharacter isoGameCharacter = IsoCamera.CamCharacter;
            float0 = IsoUtils.XToScreen(isoGameCharacter.x + this.DeferedX, isoGameCharacter.y + this.DeferedY, isoGameCharacter.z, 0);
            float1 = IsoUtils.YToScreen(isoGameCharacter.x + this.DeferedX, isoGameCharacter.y + this.DeferedY, isoGameCharacter.z, 0);
            float0 -= (float)(IsoCamera.getOffscreenWidth(this.playerIndex) / 2);
            float1 -= (float)(IsoCamera.getOffscreenHeight(this.playerIndex) / 2);
            float1 -= isoGameCharacter.getOffsetY() * 1.5F;
            float0 += (float)IsoCamera.PLAYER_OFFSET_X;
            float1 += (float)IsoCamera.PLAYER_OFFSET_Y;
        }

        this.OffX = this.TOffX = float0;
        this.OffY = this.TOffY = float1;
    }

    public void update() {
        this.center();
        float float0 = (this.TOffX - this.OffX) / 15.0F;
        float float1 = (this.TOffY - this.OffY) / 15.0F;
        this.OffX += float0;
        this.OffY += float1;
        if (this.lastOffX == 0.0F && this.lastOffY == 0.0F) {
            this.lastOffX = this.OffX;
            this.lastOffY = this.OffY;
        }

        long _long = System.currentTimeMillis();
        PAN_SPEED = 110.0F;
        float float2 = this.panTime < 0L ? 1.0F : (float)(_long - this.panTime) / 1000.0F * PAN_SPEED;
        float2 = 1.0F / float2;
        this.panTime = _long;
        IsoPlayer isoPlayer = IsoPlayer.players[this.playerIndex];
        boolean boolean0 = zombie.GameWindow.ActivatedJoyPad != null && isoPlayer != null && isoPlayer.JoypadBind != -1;
        BaseVehicle baseVehicle = isoPlayer == null ? null : isoPlayer.getVehicle();
        if (baseVehicle != null && baseVehicle.getCurrentSpeedKmHour() <= 1.0F) {
            baseVehicle.getForwardVector(this.m_lastVehicleForwardDirection);
        }

        if (Core.getInstance().getOptionPanCameraWhileDriving() && baseVehicle != null && baseVehicle.getCurrentSpeedKmHour() > 1.0F) {
            float float3 = Core.getInstance().getZoom(this.playerIndex);
            float float4 = baseVehicle.getCurrentSpeedKmHour() * BaseVehicle.getFakeSpeedModifier() / 10.0F;
            float4 *= float3;
            Vector3f vector3f = baseVehicle.getForwardVector(((BaseVehicle.Vector3fObjectPool)BaseVehicle.TL_vector3f_pool.get()).alloc());
            float float5 = this.m_lastVehicleForwardDirection.angle(vector3f) * (180.0F / (float)Math.PI);
            if (float5 > 1.0F) {
                float float6 = float5 / 180.0F / (float)PerformanceSettings.getLockFPS();
                float6 = PZMath.max(float6, 0.1F);
                this.m_lastVehicleForwardDirection.lerp(vector3f, float6, vector3f);
                this.m_lastVehicleForwardDirection.set(vector3f);
            }

            this.RightClickTargetX = (float)((int)IsoUtils.XToScreen(vector3f.x * float4, vector3f.z * float4, isoPlayer.z, 0));
            this.RightClickTargetY = (float)((int)IsoUtils.YToScreen(vector3f.x * float4, vector3f.z * float4, isoPlayer.z, 0));
            ((BaseVehicle.Vector3fObjectPool)BaseVehicle.TL_vector3f_pool.get()).release(vector3f);
            byte byte0 = 0;
            byte byte1 = 0;
            int int0 = IsoCamera.getOffscreenWidth(this.playerIndex);
            int int1 = IsoCamera.getOffscreenHeight(this.playerIndex);
            float float7 = (float)byte0 + (float)int0 / 2.0F;
            float float8 = (float)byte1 + (float)int1 / 2.0F;
            float float9 = 150.0F * float3;
            this.RightClickTargetX = (float)((int)PZMath.clamp(float7 + this.RightClickTargetX, float9, (float)int0 - float9)) - float7;
            this.RightClickTargetY = (float)((int)PZMath.clamp(float8 + this.RightClickTargetY, float9, (float)int1 - float9)) - float8;
            if (Math.abs(float4) < 5.0F) {
                float float10 = 1.0F - Math.abs(float4) / 5.0F;
                this.returnToCenter(1.0F / (16.0F * float2 / float10));
            } else {
                float2 /= 0.5F * float3;
                float float11 = IsoUtils.XToScreenExact(isoPlayer.x, isoPlayer.y, isoPlayer.z, 0);
                float float12 = IsoUtils.YToScreenExact(isoPlayer.x, isoPlayer.y, isoPlayer.z, 0);
                if (float11 < float9 / 2.0F || float11 > (float)int0 - float9 / 2.0F || float12 < float9 / 2.0F || float12 > (float)int1 - float9 / 2.0F) {
                    float2 /= 4.0F;
                }

                this.RightClickX_f = PZMath.step(
                    this.RightClickX_f, this.RightClickTargetX, 1.875F * (float)PZMath.sign(this.RightClickTargetX - this.RightClickX_f) / float2
                );
                this.RightClickY_f = PZMath.step(
                    this.RightClickY_f, this.RightClickTargetY, 1.875F * (float)PZMath.sign(this.RightClickTargetY - this.RightClickY_f) / float2
                );
                this.RightClickX = (float)((int)this.RightClickX_f);
                this.RightClickY = (float)((int)this.RightClickY_f);
            }
        } else if (boolean0 && isoPlayer != null) {
            if ((isoPlayer.IsAiming() || isoPlayer.isLookingWhileInVehicle())
                && JoypadManager.instance.isRBPressed(isoPlayer.JoypadBind)
                && !isoPlayer.bJoypadIgnoreAimUntilCentered) {
                this.RightClickTargetX = JoypadManager.instance.getAimingAxisX(isoPlayer.JoypadBind) * 1500.0F;
                this.RightClickTargetY = JoypadManager.instance.getAimingAxisY(isoPlayer.JoypadBind) * 1500.0F;
                float2 /= 0.5F * Core.getInstance().getZoom(this.playerIndex);
                this.RightClickX_f = PZMath.step(this.RightClickX_f, this.RightClickTargetX, (this.RightClickTargetX - this.RightClickX_f) / (80.0F * float2));
                this.RightClickY_f = PZMath.step(this.RightClickY_f, this.RightClickTargetY, (this.RightClickTargetY - this.RightClickY_f) / (80.0F * float2));
                this.RightClickX = (float)((int)this.RightClickX_f);
                this.RightClickY = (float)((int)this.RightClickY_f);
                isoPlayer.dirtyRecalcGridStackTime = 2.0F;
            } else {
                this.returnToCenter(1.0F / (16.0F * float2));
            }
        } else if (this.playerIndex == 0 && isoPlayer != null && !isoPlayer.isBlockMovement() && GameKeyboard.isKeyDown(Core.getInstance().getKey("PanCamera"))
            )
         {
            int int2 = IsoCamera.getScreenWidth(this.playerIndex);
            int int3 = IsoCamera.getScreenHeight(this.playerIndex);
            int int4 = IsoCamera.getScreenLeft(this.playerIndex);
            int int5 = IsoCamera.getScreenTop(this.playerIndex);
            float float13 = (float)Mouse.getXA() - ((float)int4 + (float)int2 / 2.0F);
            float float14 = (float)Mouse.getYA() - ((float)int5 + (float)int3 / 2.0F);
            float float15;
            if (int2 > int3) {
                float15 = (float)int3 / (float)int2;
                float13 *= float15;
            } else {
                float15 = (float)int2 / (float)int3;
                float14 *= float15;
            }

            float15 *= (float)int2 / 1366.0F;
            offVec.set(float13, float14);
            offVec.setLength(Math.min(offVec.getLength(), (float)Math.min(int2, int3) / 2.0F));
            float13 = offVec.x / float15;
            float14 = offVec.y / float15;
            this.RightClickTargetX = float13 * 2.0F;
            this.RightClickTargetY = float14 * 2.0F;
            float2 /= 0.5F * Core.getInstance().getZoom(this.playerIndex);
            this.RightClickX_f = PZMath.step(this.RightClickX_f, this.RightClickTargetX, (this.RightClickTargetX - this.RightClickX_f) / (80.0F * float2));
            this.RightClickY_f = PZMath.step(this.RightClickY_f, this.RightClickTargetY, (this.RightClickTargetY - this.RightClickY_f) / (80.0F * float2));
            this.RightClickX = (float)((int)this.RightClickX_f);
            this.RightClickY = (float)((int)this.RightClickY_f);
            isoPlayer.dirtyRecalcGridStackTime = 2.0F;
            IsoSprite.globalOffsetX = -1.0F;
        } else if (this.playerIndex == 0 && Core.getInstance().getOptionPanCameraWhileAiming()) {
            boolean boolean1 = !GameServer.bServer;
            boolean boolean2 = !UIManager.isMouseOverInventory() && isoPlayer != null && isoPlayer.isAiming();
            boolean boolean3 = !boolean0 && isoPlayer != null && !isoPlayer.isDead();
            if (boolean1 && boolean2 && boolean3) {
                int int6 = IsoCamera.getScreenWidth(this.playerIndex);
                int int7 = IsoCamera.getScreenHeight(this.playerIndex);
                int int8 = IsoCamera.getScreenLeft(this.playerIndex);
                int int9 = IsoCamera.getScreenTop(this.playerIndex);
                float float16 = (float)Mouse.getXA() - ((float)int8 + (float)int6 / 2.0F);
                float float17 = (float)Mouse.getYA() - ((float)int9 + (float)int7 / 2.0F);
                float float18;
                if (int6 > int7) {
                    float18 = (float)int7 / (float)int6;
                    float16 *= float18;
                } else {
                    float18 = (float)int6 / (float)int7;
                    float17 *= float18;
                }

                float18 *= (float)int6 / 1366.0F;
                float float19 = (float)Math.min(int6, int7) / 6.0F;
                float float20 = (float)Math.min(int6, int7) / 2.0F - float19;
                offVec.set(float16, float17);
                if (offVec.getLength() < float20) {
                    float17 = 0.0F;
                    float16 = 0.0F;
                } else {
                    offVec.setLength(Math.min(offVec.getLength(), (float)Math.min(int6, int7) / 2.0F) - float20);
                    float16 = offVec.x / float18;
                    float17 = offVec.y / float18;
                }

                this.RightClickTargetX = float16 * 7.0F;
                this.RightClickTargetY = float17 * 7.0F;
                float2 /= 0.5F * Core.getInstance().getZoom(this.playerIndex);
                this.RightClickX_f = PZMath.step(this.RightClickX_f, this.RightClickTargetX, (this.RightClickTargetX - this.RightClickX_f) / (80.0F * float2));
                this.RightClickY_f = PZMath.step(this.RightClickY_f, this.RightClickTargetY, (this.RightClickTargetY - this.RightClickY_f) / (80.0F * float2));
                this.RightClickX = (float)((int)this.RightClickX_f);
                this.RightClickY = (float)((int)this.RightClickY_f);
                isoPlayer.dirtyRecalcGridStackTime = 2.0F;
            } else {
                this.returnToCenter(1.0F / (16.0F * float2));
            }

            IsoSprite.globalOffsetX = -1.0F;
        } else {
            this.returnToCenter(1.0F / (16.0F * float2));
        }

        this.zoom = Core.getInstance().getZoom(this.playerIndex);
    }

    private void returnToCenter(float _float) {
        this.RightClickTargetX = 0.0F;
        this.RightClickTargetY = 0.0F;
        if (this.RightClickTargetX != this.RightClickX || this.RightClickTargetY != this.RightClickY) {
            this.RightClickX_f = PZMath.step(this.RightClickX_f, this.RightClickTargetX, (this.RightClickTargetX - this.RightClickX_f) * _float);
            this.RightClickY_f = PZMath.step(this.RightClickY_f, this.RightClickTargetY, (this.RightClickTargetY - this.RightClickY_f) * _float);
            this.RightClickX = (float)((int)this.RightClickX_f);
            this.RightClickY = (float)((int)this.RightClickY_f);
            if (Math.abs(this.RightClickTargetX - this.RightClickX_f) < 0.001F) {
                this.RightClickX = (float)((int)this.RightClickTargetX);
                this.RightClickX_f = this.RightClickX;
            }

            if (Math.abs(this.RightClickTargetY - this.RightClickY_f) < 0.001F) {
                this.RightClickY = (float)((int)this.RightClickTargetY);
                this.RightClickY_f = this.RightClickY;
            }

            IsoPlayer isoPlayer = IsoPlayer.players[this.playerIndex];
            isoPlayer.dirtyRecalcGridStackTime = 2.0F;
        }
    }

    public float getOffX() {
        return (float)((int)(this.OffX + this.RightClickX));
    }

    public float getOffY() {
        return (float)((int)(this.OffY + this.RightClickY));
    }

    public float getTOffX() {
        float _float = this.TOffX - this.OffX;
        return (float)((int)(this.OffX + this.RightClickX - _float));
    }

    public float getTOffY() {
        float _float = this.TOffY - this.OffY;
        return (float)((int)(this.OffY + this.RightClickY - _float));
    }

    public float getLastOffX() {
        return (float)((int)(this.lastOffX + this.RightClickX));
    }

    public float getLastOffY() {
        return (float)((int)(this.lastOffY + this.RightClickY));
    }

    public float XToIso(float screenX, float screenY, float floor) {
        screenX = (float)((int)screenX);
        screenY = (float)((int)screenY);
        float float0 = screenX + this.getOffX();
        float float1 = screenY + this.getOffY();
        float float2 = (float0 + 2.0F * float1) / (64.0F * (float)Core.TileScale);
        return float2 + 3.0F * floor;
    }

    public float YToIso(float screenX, float screenY, float floor) {
        screenX = (float)((int)screenX);
        screenY = (float)((int)screenY);
        float float0 = screenX + this.getOffX();
        float float1 = screenY + this.getOffY();
        float float2 = (float0 - 2.0F * float1) / (-64.0F * (float)Core.TileScale);
        return float2 + 3.0F * floor;
    }

    public float YToScreenExact(float objectX, float objectY, float objectZ, int screenZ) {
        float _float = IsoUtils.YToScreen(objectX, objectY, objectZ, screenZ);
        return _float - this.getOffY();
    }

    public float XToScreenExact(float objectX, float objectY, float objectZ, int screenZ) {
        float _float = IsoUtils.XToScreen(objectX, objectY, objectZ, screenZ);
        return _float - this.getOffX();
    }

    public void copyFrom(PlayerCamera other) {
        this.OffX = other.OffX;
        this.OffY = other.OffY;
        this.TOffX = other.TOffX;
        this.TOffY = other.TOffY;
        this.lastOffX = other.lastOffX;
        this.lastOffY = other.lastOffY;
        this.RightClickTargetX = other.RightClickTargetX;
        this.RightClickTargetY = other.RightClickTargetY;
        this.RightClickX = other.RightClickX;
        this.RightClickY = other.RightClickY;
        this.DeferedX = other.DeferedX;
        this.DeferedY = other.DeferedY;
        this.zoom = other.zoom;
        this.OffscreenWidth = other.OffscreenWidth;
        this.OffscreenHeight = other.OffscreenHeight;
    }

    public void initFromIsoCamera(int _playerIndex) {
        this.copyFrom(IsoCamera.cameras[_playerIndex]);
        this.zoom = Core.getInstance().getZoom(_playerIndex);
        this.OffscreenWidth = IsoCamera.getOffscreenWidth(_playerIndex);
        this.OffscreenHeight = IsoCamera.getOffscreenHeight(_playerIndex);
    }
}
